package leetcode_700;

/**
 *@author 周杨
 *ImageSmoother_661 图片平滑器  把图片像素点更新为最近9的的平均值
 *describe:遍历即可 很无聊 AC 91%
 *2018年9月28日 下午1:17:03
 */
public class ImageSmoother_661 {
	 public int[][] imageSmoother(int[][] M) {
	        int[][] ans = new int[M.length][M[0].length];
	        for (int i = 0; i < M.length; i++) {
	            for (int j = 0; j < M[0].length; j++) {
	                ans[i][j] = smoother(M, i, j);
	            }
	        }
	        return ans;
	    }
	    
	    public int smoother(int[][] M, int i, int j) {
	        int[] dx = {1, 0, 0, -1, -1, 1, 1, -1};
	        int[] dy = {0, 1, -1, 0, -1, 1, -1, 1};
	        int sum = M[i][j];
	        int count = 1;
	        for (int k = 0; k < 8; k++) {
	            int x = i + dx[k];
	            int y = j + dy[k];
	            if (x >= 0 && x < M.length && y >= 0 && y < M[0].length) {
	                sum += M[x][y];
	                count++;
	            }
	        }
	        return sum / count;
	    }
}
